package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.home.adapter.FightGroupAddressAdapter;
import com.meitianhui.convenitenbuy.module.home.contract.FightGroupAddressContract;
import com.meitianhui.convenitenbuy.module.home.di.DaggerHomeComponent;
import com.meitianhui.convenitenbuy.module.home.di.HomeModule;
import com.meitianhui.convenitenbuy.module.home.model.bean.FightGroupAddress;
import com.meitianhui.convenitenbuy.module.home.presenter.FightGroupAddressPresenter;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/18.
 */

public class FightGroupAddressActivity extends BaseToolbarActivity implements FightGroupAddressContract.View {
    private int mPageIndex = 1;
    private int mPageSize = 15;
    private RecyclerView mRecyclerView;
    private String longitude, latitude;
    private FightGroupAddressAdapter mAdapter;
    private View mNotDataView;
    private List<FightGroupAddress.Convenient> convenientList;
    private String mAddress;

    @Inject
    FightGroupAddressPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_fight_group_address;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerHomeComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
        setTbTitle("收货地址");
        mRecyclerView = getViewById(R.id.rv_fight_group_address);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        longitude = InfoProvider.getLongitude(this);
        latitude = InfoProvider.getLatitude(this);
        mNotDataView = getLayoutInflater().inflate(R.layout.data_empty_goods, (ViewGroup) mRecyclerView.getParent(), false);
        mAdapter = new FightGroupAddressAdapter(null);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("address", convenientList.get(position).getAddress());
                intent.putExtra("storeId", convenientList.get(position).getStores_id());
                intent.putExtra("storeName", convenientList.get(position).getStores_name());
                setResult(OpenFightGroupActivity.RESULT_INTENT, intent);
                finish();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPageIndex++;
                presenter.getFightGroupAddress(mPageIndex + "", mPageSize + "", longitude, latitude);
            }
        }, mRecyclerView);
        onRefreshData();

    }


    @Override
    protected void onRefreshData() {
        mPageIndex = 1;
        presenter.getFightGroupAddress(mPageIndex + "", mPageSize + "", longitude, latitude);
    }

    @Override
    public void getFightGroupAddressSuccess(FightGroupAddress data) {
        refreshFinish();
        setData(mPageIndex, data.getList());
    }


    private void setData(int pageIndex, List data) {
        final int size = data == null ? 0 : data.size();
        if (pageIndex == 1) {
            mAdapter.setNewData(data);
            if (size == 0) {
                mAdapter.setEmptyView(mNotDataView);
            }
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        convenientList = data;
        if (size < mPageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(false);
        } else {
            mAdapter.loadMoreComplete();
        }

    }

    @Override
    public void showError(String msg) {

    }


}
